﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Dune.Common.Utils;

namespace Dune.DAL.Common.ResultSet
{
    public class ResultSetMapper<TResult>
        where TResult : new()
    {

        IDictionary<string, string> resultProperty = new Dictionary<string, string>();


        public ResultSetMapper()
        {
        }

        public void AddMap(string resultName, string propertyName)
        {
            resultProperty[resultName] = propertyName;
        }

        public TResult Map(string[] names, object[] results)
        {

            TResult res = new TResult();

            for (int i = 0; i < names.Length; i++)
            {
                if (resultProperty.ContainsKey(names[i]))
                {
                    ReflectionUtils.SetValue(res, resultProperty[names[i]], results[i], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                }
            }

            return res;

        }

    }
}
